<?php

namespace app\common\model;


use app\common\exception\ApiException;

class Information extends BaseModel {
	public function getTagsAttr( $value ) {
		if ( ! $value ) {
			return '';
		}

		return explode( ',', $value );
	}

	public function images() {
		return $this->hasMany( 'Images', 'information_id', 'id' )->field( 'url' );
	}

	public function user() {
		return $this->belongsTo( 'User', 'user_id', 'id' )->field( 'id,nickName,avatarUrl' );

	}

	public function cat() {
		return $this->belongsTo( 'Category', 'cat_id', 'id' )->field( 'id,name,status' );
	}

	public function getInformation( $condition = [], $from = 0, $size = 10, $order = [] ) {
		if ( empty( $condition['status'] ) && ! isset( $condition['status'] ) ) {
			$condition['status'] = [ 'neq', config( 'code.status_delete' ) ];
		}
		$order['is_top'] = 'desc';
		$order           = [
			'is_top'      => 'desc',
			'create_time' => 'desc',
			'id'          => 'desc'
		];
		$result          = $this
			->with( [ 'cat', 'user', 'images' ] )
			->where( $condition )
			->order( $order )
			->limit( $from, $size )
			->select();
		if ( ! $result ) {
			throw new ApiException( '没有数据了', 200, 1 );
		}
		if ( input( 'get.total' ) == 1 ) {
			$arr['list']  = collection( $result )->hidden( [ 'cat_id', 'user_id', 'update_time' ] );
			$arr['total'] = $this->where( $condition )->count();
			$result       = $arr;

			return $result;
		}

		return collection( $result )->hidden( [ 'cat_id', 'user_id', 'update_time' ] );
	}

	public function getInformationByIds( $condition = [] ) {
		if ( empty( $condition['status'] ) && ! isset( $condition['status'] ) ) {
			$condition['status'] = [ 'neq', config( 'code.status_delete' ) ];
		}
		$result = $this
			->with( [ 'cat', 'user', 'images' ] )
			->where( $condition )
			->select();
		if ( ! $result ) {
			throw new ApiException( '没有数据了', 200, 1 );
		}

		return collection( $result )->hidden( [ 'cat_id', 'user_id', 'update_time' ] );
	}

	public function updateCount( $id, $field, $type = false ) {
		if ( ! $type ) {
			$this->get( $id )->setInc( $field );
		} else {
			$this->get( $id )->setDec( $field );
		}
	}

	public function searchResult( $keyword, $from = 0, $size = 10, $order = [ 'id' => 'desc' ] ) {
		$condition['status'] = [ 'eq', config( 'code.status_show' ) ];
		$result              = $this
			->with( [ 'cat', 'user', 'images' ] )
			->where($condition)
			->where( 'content|address|tags|phone|username', 'like', '%' . $keyword . '%' )
			->order( $order )
			->limit( $from, $size )
			->select();
		if ( ! $result ) {
			throw new ApiException( '没有数据了', 200, 1 );
		}
		if ( input( 'get.total' ) == 1 ) {
			$arr['list']  = collection( $result )->hidden( [ 'cat_id', 'user_id', 'update_time' ] );
			$arr['total'] = $this->where( $condition )->count();
			$result       = $arr;

			return $result;
		}

		return collection( $result )->hidden( [ 'cat_id', 'user_id', 'update_time' ] );
	}

	public function changeStatus( $id, $user_id, $status ) {
		$condition = [
			'id'      => $id,
			'user_id' => $user_id
		];
		$result    = $this->where( $condition )->update( [
			'status' => $status
		] );
		if ( ! $result ) {
			throw new ApiException( '更新状态失败', 400 );
		}

		return $result;
	}

	public function rePublishMessage( $id, $user_id ) {
		$condition = [
			'id'      => $id,
			'user_id' => $user_id
		];
		$result    = $this->where( $condition )->update( [
			'status'      => config( 'code.status_show' ),
			'create_time' => time()
		] );
		model( 'User' )->get( [ 'user_id' => $user_id ] )->setInc( 'publish_count' );
		if ( ! $result ) {
			throw new ApiException( '重新发布信息失败', 400 );
		}

		return $result;
	}
}